import { login } from '@/api/user.js'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { getInfo, staffPhoto } from '@/api/user.js'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    updateToken(state, token) {
      state.token = token
      // localStorage.setItem('token', token)
      setToken(token)
    },
    getInfo(state, userinfo) {
      state.userInfo = userinfo
    },
    delToken(state) {
      state.token = ''
      removeToken()
    }

  },
  actions: {
    async userLogin(context, loginForm) {
      const res = await login(loginForm)
      context.commit('updateToken', res.data)
    },
    async getUserProfile(context) {
      const { data: res } = await getInfo()
      const { data: photo } = await staffPhoto(res.userId)
      // console.log(res)
      context.commit('getInfo', { ...res, ...photo })
    },
    delUserInfo(context) {
      context.commit('delToken')
      context.commit('getInfo', '')
    }
  }
}
